# # API文档地址： https://gitee.com/paddlepaddle/PaddleOCR#/paddlepaddle/PaddleOCR/blob/release/2.4/doc/doc_ch/quickstart.md
# from paddleocr.paddleocr import PaddleOCR
# import os

# LANS = ['ch', 'en', 'japan', 'korean']
# class PPOcr:
#     ocr = None
#     lang = 'japan'
#     use_angle_cls = False

#     def __init__(self, config):
#         self.initOptions(config)
        
#     def initOcr(self, config):
#         self.ocr = PaddleOCR(lang=self.lang, use_angle_cls=self.use_angle_cls)

#     def initOptions(self, config):
#         t = config['SOURCE_LANGUAGE']
#         if t == 0:
#             pass
#         else:
#             self.lang = LANS[t-1]
#         self.initOcr(config)

#     def ocrPath(self, path):
#         # 不使用方向检测器
#         result = self.ocr.ocr(path, cls=False)
#         return self.parse(result)

#     def parse(self, res):
#         word = ''
#         for line in res:
#             word += line[1][0]
#         return word

# 接口文档：https://www.jaided.ai/easyocr/documentation/
# 模型下载地址：https://www.jaided.ai/easyocr/modelhub/
import easyocr
import os
__PATH__ = os.path.dirname(os.path.abspath(__file__))
__MODEL_PATH__ = os.path.join(__PATH__, '../model')
# ch_tra 繁体
LANS = ['ch_sim','en','ja','ko']
class EasyOcr:
    lang = "en"
    client = None

    def __init__(self, config):
        self.initOptions(config)

    def initOcr(self, config):
        self.client = easyocr.Reader(
            lang_list = [self.lang,'en'],   # 识别语言，可多项
            download_enabled = True,        # 自动下载模型
            model_storage_directory = __MODEL_PATH__    # 设置模型保存路径
        )

    def initOptions(self, config):   
        t = config['SOURCE_LANGUAGE']
        self.changeSource(t)
        self.initOcr(None)

    def ocrPath(self, path):
        return self.parse(self.client.readtext(path))

    # 修改识别语言
    def changeSource(self, lan_key):
            self.lang = LANS[lan_key-1]

    # 解析识别结果
    def parse(self, arr):
        res = ''
        for line in arr:
            res += line[1]+' '
        return res